﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CFM.DTO;
using CFM.DAL;

namespace CFM.API
{
    class ProviderAPI
    {
        private DataHelper _helper = null;
        public ProviderAPI()
        {
            _helper = new DataHelper();  
        }

        public List<Provider> GetAll()
        {
            string sql = "select * from Provider";
            SqlDataReader dr = _helper.ExcuteDataReader(sql, null);
            return _helper.MapDataReaderToObject<Provider>(dr);
        }
        public bool Create(Provider provider)
        {
            string sql = "insert into Provider(ProviderId,ProviderName,Description,Address,Phone) values(@ProviderId,@ProviderName,@Description,@Address,@Phone)";
            SqlParameter[] prs = 
            {
                new SqlParameter("@ProviderId",provider.ProviderId),
                new SqlParameter("@ProviderName",provider.ProviderName),
                new SqlParameter("@Description",provider.Description),
                new SqlParameter("@Address",provider.Address),
                new SqlParameter("@Phone",provider.Phone)
            };
            return _helper.ExcuteNonQuery(sql, prs);
        }

        public bool Update(Provider provider)
        {
            string sql = "update Provider set ProviderName = @ProviderName,Description=@Description,Address=@Address,Phone=@Phone where ProviderId = @ProviderId";
            SqlParameter[] prs = 
            {
                new SqlParameter("@ProviderId",provider.ProviderId),
                new SqlParameter("@ProviderName",provider.ProviderName),
                new SqlParameter("@Description",provider.Description),
                new SqlParameter("@Address",provider.Address),
                new SqlParameter("@Phone",provider.Phone)
            };
            return _helper.ExcuteNonQuery(sql, prs);
        }

        public bool Delete(string ProviderId)
        {
            string sql = "delete Provider where ProviderId = @ProviderId";
            SqlParameter[] prs = 
            {
                new SqlParameter("@ProviderId",ProviderId)
            };
            return _helper.ExcuteNonQuery(sql, prs);
        }
    }
}
